package net.xinqushi.gc;

import java.lang.ref.WeakReference;

/**
 * 静态回收：
 * 回收类时，如果静态内容只有变量，那么直接回收。(常量池有关)
 * 如果静态内容包含引用（StrongReference），那么不会回收。此处参考
 * @see ReferenceDemo
 */
public class StaticGC {

    public static void main(String[] args) {
        StaticClass staticClass = new StaticClass();
        staticClass = null;
        while(true){
            System.gc();
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("gc");
        }
    }

    static class StaticClass{
        static int a = 2; // static variable
        static MixBeanGC.Person person = new MixBeanGC.Person();//strong reference
        static WeakReference<MixBeanGC.Person> weakReference = new WeakReference<MixBeanGC.Person>(new MixBeanGC.Person());

        @Override
        protected void finalize() throws Throwable {
            super.finalize();
            System.out.println(this.getClass() + " is finalized");
        }
    }
}
